﻿//-----------------------------------------------------------------------
// <copyright file="Target.cs">
//     Copyright (c) Nicole Calinoiu. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Xml;

namespace Bordecal.FxCop.Sdk.Testing
{
	/// <summary>
	/// Base class for .NET entities that are screened by FxCop.
	/// </summary>
	internal abstract class Target
	{
		internal Target(Target parent, XmlElement element)
		{
			if (element == null)
			{
				throw new ArgumentNullException("element");
			}

			this.Parent = parent;
			this.Name = element.GetAttribute("Name");
		}

		internal Target(Target parent, string name)
		{
			if (name == null)
			{
				throw new ArgumentNullException("name");
			}

			this.Parent = parent;
			this.Name = name;
		}

		/// <summary>
		/// Gets the <see cref="Target"/> of which the current <see cref="Target"/> is a child.
		/// </summary>
		internal Target Parent { get; private set; }

		/// <summary>
		/// Gets or sets the name of the <see cref="Target"/>.
		/// </summary>
		protected internal string Name { get; protected set; }

		/// <summary>
		/// Returns a hash code for this instance.
		/// </summary>
		/// <returns>
		/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 
		/// </returns>
		public override int GetHashCode()
		{
			int result = this.Name.GetHashCode();
			if (this.Parent != null)
			{
				result ^= this.Parent.GetHashCode();
			}

			return result;
		}

		internal virtual bool Equals(Target target)
		{
			bool result = false;

			if ((target != null) && (target.GetType() == target.GetType()))
			{
				if (string.Equals(target.Name, this.Name, StringComparison.Ordinal))
				{
					if ((this.Parent == null) && (target.Parent == null))
					{
						result = true;
					}
					else if (this.Parent != null)
					{
						result = this.Parent.Equals(target.Parent);
					}
				}
			}

			return result;
		}
	}
}